//
//  TabBarProvider.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/10/27.
//

import Foundation
import ESTabBarController
import MYModule
import HandyJSON
import UIKit

class TabBarConfig: HandyJSON {
    var controller: ViewController = ViewController()
    var animationJsonName: String = ""
    var title: String = ""
    var defaultImage: UIImage? = nil
    var selectedImage: UIImage? = nil
    
    required init() {
        
    }
    
    init(controller: ViewController = ViewController(), animationJsonName: String = "", title: String = "", defaultImage: UIImage? = nil, selectedImage: UIImage? = nil) {
        self.controller = controller
        self.animationJsonName = animationJsonName
        self.title = title
        self.defaultImage = defaultImage
        self.selectedImage = selectedImage
    }
    
}

enum TabBarProvider {
    
    static func lottieSytle() -> ESTabBarController {
        
        let tabBarController = TabBarController()
        
        tabBarController.tabBar.barTintColor = UIColor.assist_FFFFFF
        
        
        if #available(iOS 13.0, *) {
            let appeaper = UITabBarAppearance()
            
            appeaper.shadowColor = UIColor.init(hex: "#EEEEE")
            appeaper.backgroundColor = UIColor.background
            appeaper.backgroundEffect = nil
            
            tabBarController.tabBar.standardAppearance = appeaper
            if #available(iOS 15.0, *) {
                tabBarController.tabBar.scrollEdgeAppearance = appeaper
            }
        }
       
        let HomeController = navigationBarController(rootViewController: HomeViewController())
        let sleepController = navigationBarController(rootViewController: SleepViewController())
        let activityController = navigationBarController(rootViewController: ActivityHomeController())
        let profileController = navigationBarController(rootViewController: MyHomeController())

        let todayLottieView = LottieAnimateContentView()
        todayLottieView.setAnimationName("ani_watchface")

        let sleepLottieView = LottieAnimateContentView()
        sleepLottieView.setAnimationName("loading")

        let activityLottieView = LottieAnimateContentView()
        activityLottieView.setAnimationName("ani_watchface")

        let profileLottieView = LottieAnimateContentView()
        profileLottieView.setAnimationName("loading")


        HomeController.tabBarItem = item(title: nil,
                                                 image: UIImage(named: "ic_tab_today_n"),
                                         selectedImage: UIImage(named: "ic_tab_today_h"), selectedColor: UIColor.theme)
        
       
        activityController.tabBarItem = item(title: nil,
                                                     image: UIImage(named: "ic_tab_activity_n"),
                                                     selectedImage: UIImage(named: "ic_tab_activity_h"), selectedColor: UIColor.theme)
        
        sleepController.tabBarItem = item(title: nil,
                                                  image: UIImage(named: "ic_tab_sleep_n"),
                                                  selectedImage: UIImage(named: "ic_tab_sleep_h"), selectedColor: UIColor.theme)
        
        profileController.tabBarItem = item(title: nil,
                                                    image: UIImage(named: "ic_tab_user_n"),
                                                    selectedImage: UIImage(named: "ic_tab_user_h"), selectedColor: UIColor.theme)
        tabBarController.viewControllers = [HomeController,activityController, sleepController,  profileController]

        return tabBarController
    }
    
    
    static func item(title: String?, image: UIImage?, selectedImage: UIImage?, selectedColor: UIColor) ->ESTabBarItem {
        
        let item = ESTabBarItem(title: title, image: image, selectedImage: selectedImage)
        item.contentView.renderingMode = .alwaysOriginal
        item.contentView.highlightTextColor = selectedColor
        
        return item
        
    }
    
   static func navigationBarController(rootViewController: UIViewController) ->NavigationController {
        
        let nav = NavigationController(rootViewController: rootViewController)
        
        nav.navigation.configuration.barTintColor = UIColor.background
        nav.navigation.configuration.tintColor = UIColor.assist_222222
        
        return nav
        
    }
    
//    public func initConfigs() -> ESTabBarController {
//        let todayConfig = TabBarConfig(controller: HomeViewController(), animationJsonName: "ani_watchface")
//        let sleepConfig = TabBarConfig(controller: SleepHomeController(), animationJsonName: "loading")
//        let activityConfig = TabBarConfig(controller: RunDetailController(), animationJsonName: "ani_watchface")
//        let profileConfig = TabBarConfig(controller: MyHomeController(), animationJsonName: "loading")
//        return self.setupTabBarController([todayConfig, sleepConfig, activityConfig, profileConfig])
//    }
//
//    private func setupTabBarController(_ configs: [TabBarConfig]) -> ESTabBarController {
//        let tabBarController = ESTabBarController()
//        configs.forEach { config in
//            let lottieView = LottieAnimateContentView()
//            lottieView.setAnimationName("ani_watchface")
//            let nav = NavigationController(rootViewController: config.controller)
//            nav.tabBarItem = ESTabBarItem.init(lottieView, title: config.title, image: config.defaultImage, selectedImage: config.selectedImage)
//            tabBarController.viewControllers?.append(nav)
//        }
//        return tabBarController
//    }
}
